package com.lfy.modules.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lfy.modules.common.config.sys.SysDictionaries;
import com.lfy.modules.blog.domain.dto.dictionaries.DictionariesDTO;
import com.lfy.modules.blog.domain.group.SaveGroup;
import com.lfy.modules.blog.domain.group.UpdateGroup;
import com.lfy.modules.common.anno.Authentication;
import com.lfy.modules.common.domain.PageResultVO;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.utils.PageUtil;
import com.lfy.modules.common.domain.entity.SysDictionariesEntity;
import com.lfy.modules.common.mapper.SysDictionariesMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/diction")
public class DictionController {
    @Resource
    private SysDictionariesMapper sysDictionariesMapper;
    @GetMapping("/list")
    @Authentication("sys:get:diction")
    public R<PageResultVO<SysDictionariesEntity>> list(@RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum") Integer pageNum) {
        Page<SysDictionariesEntity> sysDictionariesEntityPage = new Page<>(pageNum, pageSize);
        Page<SysDictionariesEntity> page = sysDictionariesMapper.selectPage(sysDictionariesEntityPage, null);
        PageResultVO<SysDictionariesEntity> result = PageUtil.convert2PageResult(page);
        return R.success(result);
    }
    @PostMapping("/save")
    @Authentication("sys:save:diction")
    public R<Void> save(@RequestBody @Validated(SaveGroup.class) DictionariesDTO dto) {
        SysDictionariesEntity sysDictionariesEntity = new SysDictionariesEntity();
        BeanUtils.copyProperties(dto, sysDictionariesEntity);
        sysDictionariesMapper.insert(sysDictionariesEntity);
        SysDictionaries.putValue(dto.getDictionariesKey(), dto.getDictionariesValue());
        return R.success();
    }
    @PostMapping("/update")
    @Authentication("sys:modify:diction")
    public R<Void> update(@RequestBody @Validated(UpdateGroup.class) DictionariesDTO dto) {
        SysDictionariesEntity sysDictionariesEntity = new SysDictionariesEntity();
        BeanUtils.copyProperties(dto, sysDictionariesEntity);
        sysDictionariesMapper.updateById(sysDictionariesEntity);
        SysDictionaries.putValue(dto.getDictionariesKey(), dto.getDictionariesValue());
        return R.success();
    }
    @PostMapping("/remove/{id}")
    @Authentication("sys:delete:diction")
    public R<Void> delete(@PathVariable("id") Integer id) {
        sysDictionariesMapper.deleteById(id);

        return R.success();
    }

}
